#include <list>
#include <vector>
#include <algorithm>
#include <misc.h>
#include "filterio.h"
#include "filter.hpp"
#include "defines.hpp"
filterIO::filterIO( io_type ht , filterList* l  )
{
	m_type      = ht;
	ptList.push_back( l );
}

filterIO::~filterIO()
{
    //dtor
}

void filterIO::OnRecv( size_t len , const char * data , err_code err )
{
    	if( ptList.size() == 0 ) return;

    	if( m_type == head ){
		for( size_t i = 0; i < ptList.size(); i ++ ){
				filterList::iterator it = ptList[ i ]->begin();
			if( it != ptList[ i ]->end() ){
				(*it)->forward( it , len , data , err );
			}
		}
	}else if( m_type == tail ){
		for( size_t i = 0; i < ptList.size(); i ++ ){
			filterList::reverse_iterator it = ptList[ i ]->rbegin();
			if( it != ptList[ i ]->rend() ){
				(*it)->backward( it , len , data , err );
			} // if
		} // for
    	}// else if
}

bool filterIO::forward( filterList::iterator it , size_t len , const char * data , err_code err )
{
	bool                                ret = false;
	char                              * new_data = nullptr;
	size_t                              rlen;
	err_code                            err1 = err;

	// 构造一个能够检查所有链的过滤器
	std::vector< filterList::iterator>  it_end;
	for( size_t i = 0; i < ptList.size(); i ++ ){
		it_end.push_back( ptList[ i ]->end() );
	}
	// 执行任务
	ret = on_forward( len , data , err1 , rlen , &new_data );
	if( ret == true ){
		// 执行下一个过滤器操作
		if( std::none_of( it_end.begin() , it_end.end() , [it](filterList::iterator it1 )->bool{ return it1 == it; } ) ){
			it ++;
			if( std::none_of( it_end.begin() , it_end.end() , [it](filterList::iterator it1 )->bool{ return it1 == it; } ) )
				ret = (*it)->forward( it , rlen , new_data , err1 );
		}else{
			ret = false;
		}
	}

	if( new_data != nullptr && data != new_data)
		free( new_data );

	return ret;
}

bool filterIO::backward( auto it , size_t len , const char * data , err_code err )
{
	bool                  ret = false;
	char                * new_data = nullptr;
	size_t                rlen;
	err_code              err1 = err;
	std::vector<filterList::reverse_iterator>   it_end;

	// 构造一个能够检查所有的过滤器链头的表
	for( size_t i = 0; i < ptList.size(); i ++ ){
		it_end.push_back( ptList[ i ]->rend() );
	}
	// 执行操作
	__show_line_1("\n%s len0 = %ld, s_data[0] = %ld, s_data[1] = %ld,s_data[2] = %ld,s_data[3] = %ld %s\n", TRED, len, data[0], data[1], data[2], data[3], TNORMAL);
	ret = on_backward( len , data , err1 , rlen , &new_data );
	
	// 执行下一个过滤器操作
	if( ret ){
		if( std::none_of( it_end.begin() , it_end.end() , [it](filterList::reverse_iterator it1 )->bool{return it1 == it; } ) ){
			it ++;
		if( std::none_of( it_end.begin() , it_end.end() , [it](filterList::reverse_iterator it1 )->bool{return it1 == it; } ) )
			ret = (*it)->backward( it , rlen , new_data , err1 );
		}else{
			ret = false;
		}
	}else{
		ret = false;
	}
	if( new_data != nullptr && new_data != data )
		free( new_data );

	return ret;
}
